package com.shengde.jinwawaforleader.model.myJob.teachResource;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.model.myJob.adapter.MingshiAdapter;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProBookList;

import java.util.ArrayList;
import java.util.List;

/**
 * 教学资源
 * Created by Administrator on 2016/9/1 0001.
 */
public class TeachDataActivity extends BaseActivity {
	private TextView tv_back;
	private TextView tv_title;
	private TextView fill_add;
	private TeachDataActivity activity;
	private Button bt1, bt2, bt3, bt4, bt5, bt6;
	public List<ProBookList.BooklistEntity> curList = new ArrayList<ProBookList.BooklistEntity>();;//所有书本
	public List<ProBookList.BooklistEntity> allList = new ArrayList<ProBookList.BooklistEntity>();;//所有书本
	private ListView listview;
	private MingshiAdapter adapter;

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.teach_data_activity);
		activity = TeachDataActivity.this;
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView)findViewById(R.id.fill_add);
		bt1 = (Button) findViewById(R.id.bt1);
		bt2 = (Button) findViewById(R.id.bt2);
		bt3 = (Button) findViewById(R.id.bt3);
		bt4 = (Button) findViewById(R.id.bt4);
		bt5 = (Button) findViewById(R.id.bt5);
		bt6 = (Button) findViewById(R.id.bt6);
		listview = (ListView) findViewById(R.id.today_list);
	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.teach_data));
		fill_add.setText(getString(R.string.shifankeshipin));
		bt1.setTag(1);
		bt2.setTag(2);
		bt3.setTag(3);
		bt4.setTag(4);
		bt5.setTag(5);
		bt6.setTag(6);
		adapter = new MingshiAdapter(curList, this);
		listview.setAdapter(adapter);
		BaseProtocol.getInstance().getBookList(activity);
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		fill_add.setOnClickListener(this);
		bt1.setOnClickListener(this);
		bt2.setOnClickListener(this);
		bt3.setOnClickListener(this);
		bt4.setOnClickListener(this);
		bt5.setOnClickListener(this);
		bt6.setOnClickListener(this);

		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				Intent aa = new Intent();
				aa.putExtra("id",position+1);
				aa.setClass(activity, MingshiDetailActivity.class);
				activity.startActivity(aa);
			}
		});
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.fill_add:
				Intent aa = new Intent();
				aa.setClass(activity,
						MingshiShipinListActivity.class);
				aa.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(aa);
			    break;
			case R.id.bt1:
			case R.id.bt2:
			case R.id.bt3:
			case R.id.bt4:
			case R.id.bt5:
			case R.id.bt6:
				int tag = (Integer) view.getTag();
				selectButton(tag);
				displayPage(tag - 1);
				break;
		}
	}

	private Button getBtnByTag(int tag) {
		if (tag == 1)
			return bt1;
		else if (tag == 2)
			return bt2;
		else if (tag == 3)
			return bt3;
		else if (tag == 4)
			return bt4;
		else if (tag == 5)
			return bt5;
		else if (tag == 6)
			return bt6;

		return null;
	}

	private void selectButton(int tag) {
		if (tag < 1 || tag > 6)
			return;
		int selectTextColor = getResources().getColor(R.color.white_color);
		int unselectTextColor = getResources().getColor(R.color.black);
		int select1BgRec = 0, unselect1BgRec = 0, select6BgRec = 0, unselect6BgRec = 0;

		int selectBgColor = 0, unselectBgColor = 0;
		selectBgColor = getResources().getColor(R.color.app_color);
		unselectBgColor = getResources().getColor(R.color.white_color);
		select1BgRec = R.drawable.class_left_selected;
		unselect1BgRec = R.drawable.class_left_un;
		select6BgRec = R.drawable.class_right_selected;
		unselect6BgRec = R.drawable.class_right_un;
		Button btn = getBtnByTag(tag);
		//选择btn
		if (tag == 1) {
			btn.setBackgroundResource(select1BgRec);
		} else if (tag == 6) {
			btn.setBackgroundResource(select6BgRec);
		} else {
			btn.setBackgroundColor(selectBgColor);
		}
		btn.setTextColor(selectTextColor);
		//反选其他btn
		for (int i = 1; i < 7; i++) {
			if (i == tag)
				continue;
			Button tmpBt = getBtnByTag(i);
			if (i == 1) {
				tmpBt.setBackgroundResource(unselect1BgRec);
			} else if (i == 6) {
				tmpBt.setBackgroundResource(unselect6BgRec);
			} else {
				tmpBt.setBackgroundColor(unselectBgColor);
			}
			tmpBt.setTextColor(unselectTextColor);
		}
	}

	Fragment fragment = null;

	int curPage = -1;
	List<Integer> allGradidList = new ArrayList<Integer>();

	public void parseData(ProBookList resp) {
		if (resp == null)
			return;
		if (resp.getBooklist() != null && resp.getBooklist().size() > 0) {
			this.allList.clear();
			this.allList.addAll(resp.getBooklist());
		}
		if (resp.getGradelist() != null && resp.getGradelist().size() > 0) {
			for (ProBookList.GradelistEntity info : resp.getGradelist()) {
				allGradidList.add(info.getId());
			}
		}
	}

	public List<ProBookList.BooklistEntity> getPageData(int gradeid) {
		List<ProBookList.BooklistEntity> tmpLst = new ArrayList<ProBookList.BooklistEntity>();
		if (this.allList.size() < 1)
			return tmpLst;
		for (ProBookList.BooklistEntity info : allList) {
			if (info.getGradeid() != gradeid)
				continue;
			tmpLst.add(info);
		}
		return tmpLst;
	}

	public void displayPage(int page) {
		if (allGradidList.size() < 1)
			return;
		if (curPage == page)
			return;
		int gradeid = allGradidList.get(page);
		curList.clear();
		curList.addAll(getPageData(gradeid));
		adapter.notifyDataSetChanged();
		curPage = page;
	}
}
